package com.cysyz.mylib.sms.impl;

import com.cysyz.mylib.commonservice.servicesupport.CommonServiceSupport;
import com.cysyz.mylib.dao.Conds;
import com.cysyz.mylib.dao.PagingResult;
import com.cysyz.mylib.sms.SmsSendLogAble;
import com.cysyz.mylib.sms.SmsSendLogService;
import com.cysyz.mylib.utils.myassert.MyAssert;

public class SmsSendLogServiceImpl extends CommonServiceSupport implements SmsSendLogService{
	
	@Override
	public <T extends SmsSendLogAble> void saveSendLog(Class<T> clazz, String[] phoneNumsArray, String[] contents) {
		MyAssert.hasNoParamConstructor(clazz);
		MyAssert.notNull(phoneNumsArray);
		MyAssert.notNull(contents);
		
		if ((phoneNumsArray.length >=1 && contents.length == 1)
			|| (phoneNumsArray.length >1 && contents.length > 1 && phoneNumsArray.length == contents.length)) {
			for (int i = 0 ; i < phoneNumsArray.length ; i ++) {
				SmsSendLogAble sendLog = null;
				try {
					sendLog = (SmsSendLogAble) clazz.newInstance();
				} catch (InstantiationException | IllegalAccessException e) {
					throw new RuntimeException("继承SmsSendLogAble的类创建失败", e);
				}
				sendLog.setMobile(phoneNumsArray[i]);
				if (phoneNumsArray.length >=1 && contents.length == 1) {
					sendLog.setContent(contents[0]);
				} else if (phoneNumsArray.length >1 && contents.length > 1 && phoneNumsArray.length == contents.length) {
					sendLog.setContent(contents[i]);
				}
				sendLog.setSubmitTime(Long.valueOf(System.currentTimeMillis() / 1000).intValue());
				sendLog.setSendTime(Long.valueOf(System.currentTimeMillis() / 1000).intValue());
				sendLog.setCreateTime(System.currentTimeMillis());
				sendLog.setStatus("y");
				this.save(sendLog);
			}
		}
	}

	@Override
	public int findSmsSendLogCount() {
		
		return (int) this.getCount(SmsSendLogAble.class);
	}

	@Override
	public <T extends SmsSendLogAble> PagingResult<T> findSmsSendLogListByCond(Class<T> clazz, String mobile,
			String status, String sendTimeStart, String sendTimeEnd, int page,
			int pageSize) {
			Conds conds = new Conds(clazz.getName().toLowerCase());
			conds.addFilter("mobile", mobile);
			conds.addFilter("status", status);
			conds.addFilter("sendTime", ">=", sendTimeStart);
			conds.addFilter("sendTime", "<", sendTimeEnd);
		return this.listPaging(clazz, conds, page, pageSize);
	}
}
